<template>
	<div>
		<el-card shadow="hover">
			<div class=" mb15">
				<el-form :model="tableData.param" ref="queryRef" :inline="true" label-width="100px">
					<el-row>
						<el-col :span="8" >
							<el-form-item label="申请时间">
								<el-date-picker v-model="tableData.dateRange" type="datetimerange" size="default"
									style="width: 300px" range-separator="-" start-placeholder="开始日期"
									end-placeholder="结束日期"></el-date-picker>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item label="所属企业" prop="companyName">
								<el-input v-model="tableData.param.companyName" placeholder="请输入所属企业" clearable
									@keyup.enter.native="getList" />
							</el-form-item>
						</el-col>
						
						<el-col :span="8" >
							<el-form-item>
								<el-button type="primary" @click="getList"><el-icon>
										<Search />
									</el-icon>搜索</el-button>
								<el-button @click="resetQuery(queryRef)"><el-icon>
										<Refresh />
									</el-icon>重置</el-button>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
				<el-row :gutter="10" class="mb8">
					<el-col :span="1.5">
						<el-button type="primary" v-if="permissionStore.isPermissionsBtn('005002001')" :disabled="multiple" @click="handleSubmit(null)">一键提交</el-button>
					</el-col>
				</el-row>
			</div>
			<el-table v-loading="loading" :data="tableData.data" @selection-change="handleSelectionChange">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="注册手机号" align="center" prop="phone" min-width="100px" />
				<el-table-column label="所属企业" align="center" prop="companyName" min-width="100px" />
				<el-table-column label="姓名" align="center" prop="realName" min-width="100px" />
				<el-table-column label="身份证号" align="center" prop="cardId" min-width="100px" />
				<el-table-column label="申请时间" align="center" prop="createTimeTxt" min-width="100px" />
				<el-table-column label="操作" align="center" class-name="small-padding" min-width="200px" fixed="right">
					<template #default="scope">
						<el-button type="primary"  v-if="permissionStore.isPermissionsBtn('005002003')" link @click="handleUpdate(scope.row)"><el-icon>
								<View />
							</el-icon>详情</el-button>
						<el-button type="primary"  v-if="permissionStore.isPermissionsBtn('005002002')" link @click="handleSubmit(scope.row)">提交</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination v-show="tableData.total > 0" :total="tableData.total" v-model:page="tableData.param.pageNumber"
				v-model:limit="tableData.param.pageLength" @pagination="getList" />
		</el-card>
		<detailView ref="detailRef" @getList="getList"></detailView>
	</div>
</template>
<script lang="ts">
import { seatRealListApi,seatAuditApi} from '@/api/auditTask/seatAuthentication';
import { toRefs, reactive, onMounted, ref, defineComponent, getCurrentInstance, toRaw } from 'vue';
import { ElMessageBox, ElMessage, FormInstance } from 'element-plus';
import { usePermissionStore } from '@/store/modules/permission'
import {updateParamsWithDateRange,formatDateTime} from '@/utils/index'
import detailView from './component/detail.vue';
export default defineComponent({
	name: 'seatAuthenticationList', //坐席实名审核列表
	components: {
		detailView,
	},
	setup() {
		const { proxy } = <any>getCurrentInstance();
		const loading = ref(false);
		const queryRef = ref();
		const detailRef = ref();
		const permissionStore = usePermissionStore()
		
		const state = reactive({
			ids: [],
			multiple: true,
			tableData: {
				data: [],
				total: 0,
				loading: false,
				dateRange:[],
				param: {
					pageNumber: 1,
					pageLength: 10,
					endTime: null,
					companyName: null,
				},
			},
		});
		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		// 初始化表格数据
		const initTableData = () => {
			getList();
		};
		/** 重置按钮操作 */
		const resetQuery = (formEl: FormInstance | undefined) => {
			if (!formEl) return;
			formEl.resetFields();
			state.tableData.dateRange=[]
			getList();
		};
		// 获取列表数据
		const getList = () => {
			loading.value = true;
			const obj = updateParamsWithDateRange(state.tableData.dateRange)
			Object.assign(state.tableData.param, obj);
			seatRealListApi(state.tableData.param).then((res: any) => {
				let list = res.data.list ?? [];
				state.tableData.data = list.map(item=>{
					item.createTimeTxt=formatDateTime(item.createTime)
					return item
				})
				state.tableData.total = res.data.total;
				loading.value = false;
			});
		};
		
		// 多选框选中数据
		const handleSelectionChange = (selection) => {
			state.ids = selection.map((item) => item.id);
			state.multiple = !selection.length;
		};
		
		
		const handleSubmit = (row) => {
			let msg = '你确定要提交所选数据吗？';
			let id: number[] = [];
			if (row) {
				msg = `此操作将提交[${row.phone}]账号，是否继续?`;
				id = [row.id];
			} else {
				id = state.ids;
			}
			if (id.length === 0) {
				ElMessage.error('请选择要提交的数据。');
				return;
			}
			ElMessageBox.confirm(msg, '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					seatAuditApi({id}).then(() => {
						ElMessage.success('提交成功');
						getList();
					});
				})
				.catch(() => { });
		};
		const handleUpdate = (row) => {
			detailRef.value.openDialog(row.seatId,row.companyName,row.id);
		};
		return {
			proxy,
			loading,
			queryRef,
			resetQuery,
			getList,
			detailRef,
			handleSelectionChange,
			handleSubmit,
			handleUpdate,
			permissionStore,
			...toRefs(state),
		};
	},
});
</script>
<style lang="scss" scoped>
.colBlock {
	display: block;
}

.colNone {
	display: none;
}
</style>
